package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/checkgroup")
public class CheckGroupController {

    // dubbo消费者
    @Reference
    CheckGroupService checkGroupService;


    // 新增检查组（返回 public Result(boolean flag, String message)）
    @RequestMapping(value = "/add")
    public Result add(@RequestBody CheckGroup checkGroup,@RequestParam(value = "checkitemIds") Integer [] checkitemIds){
        try {
            checkGroupService.add(checkGroup,checkitemIds);
            // 成功
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    // 查询检查组列表
    @RequestMapping(value = "/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = checkGroupService.findPage(queryPageBean.getQueryString(),
                    queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize());
        return pageResult;

    }

    // 使用id查询检查组，进行表单回显
    @RequestMapping(value = "/findById")
    public Result findById(Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);
        if(checkGroup!=null){
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }
        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    // 使用检查组id，查询检查组和检查项的中间表，获取检查项的集合，存放id的值
    @RequestMapping(value = "/findCheckItemListByCheckGroupId")
    public List<Integer> findCheckItemListByCheckGroupId(Integer id){
        List<Integer> list = checkGroupService.findCheckItemListByCheckGroupId(id);
        if(list!=null && list.size()>0){
            return list;
        }
        return null;
    }

    // 编辑检查组（返回 public Result(boolean flag, String message)）
    @RequestMapping(value = "/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,@RequestParam(value = "checkitemIds") Integer [] checkitemIds){
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
            // 成功
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }
    // 查询所有检查组
    @RequestMapping(value = "/findAll")
    public Result findAll(){
        try {
            List<CheckGroup> list = checkGroupService.findAll();
            // 成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            // 成功
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

}
